<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="queryParam.collegeName"
        placeholder="搜索学院名"
        prefix-icon="el-icon-search"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >搜索</el-button>
      <el-button
        class="filter-item"
        icon="el-icon-refresh-left"
        @click="handleResetFilter"
      >重置</el-button>
    </div>

    <el-table
      v-loading="loading"
      :data="collegeList"
      border
      highlight-current-row
      @sort-change="sortChangeCollegeList"
    >
      <el-table-column label="学院号" prop="id" sortable="custom" />
      <el-table-column label="学院名" prop="collegeName" />
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button
            plain
            type="primary"
            icon="el-icon-edit"
            @click.native="handleShowUpdateCollegeDialog(row)"
          />
          <el-button
            plain
            type="danger"
            icon="el-icon-delete"
            @click="handleDeleteCollege(row)"
          />
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page-num.sync="queryParam.pageNum"
      :page-size.sync="queryParam.pageSize"
      @pagination="pageCollegeList"
    />

    <el-dialog
      title="修改学院"
      :visible.sync="showDialog"
      :append-to-body="true"
      @close="showDialog = false"
    >
      <el-form ref="collegeFrom" label-width="80px" :model="collegeFrom" :rules="rules">
        <el-form-item label="学院号" prop="id">
          <el-input
            ref="id"
            v-model="collegeFrom.id"
            :disabled="true"
            type="text"
          />
        </el-form-item>
        <el-form-item label="学院名" prop="collegeName">
          <el-input
            ref="collegeName"
            v-model="collegeFrom.collegeName"
            placeholder="请输入学院名"
            type="text"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleUpdateCollege()">确认修改</el-button>
          <el-button @click="showDialog = false">取消修改</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { Pagination } from '@/views/admin/import'
import { collegeNamePattern } from '@/utils/validate'
import { pageCollegeList, updateCollege, deleteCollege } from '@/api/admin/college'

export default {
  name: 'ViewCollege',
  components: { Pagination },
  data() {
    return {
      queryParam: {
        pageNum: 1,
        pageSize: 5,
        orderField: undefined,
        orderRule: 'desc',
        collegeName: ''
      },
      collegeFrom: {
        id: '',
        collegeName: ''
      },
      rules: {
        collegeName: [
          { required: true, message: '请输入学院名', trigger: 'blur' },
          { pattern: collegeNamePattern, message: '学院名必须为中文不超过30个字符', trigger: 'blur' }
        ]
      },
      loading: false,
      showDialog: false,
      collegeList: [],
      total: 0
    }
  },
  created() {
    this.pageCollegeList()
  },
  methods: {
    /**
     * 获取学院信息
     */
    async pageCollegeList() {
      this.loading = true
      const data = await pageCollegeList(this.queryParam)
      this.collegeList = data.records
      this.total = data.total
      this.loading = false
    },
    /**
     * 搜索学院信息
     */
    handleFilter() {
      this.queryParam.pageNum = 1
      this.pageCollegeList()
    },
    /**
     * 重置搜索条件
     */
    handleResetFilter() {
      this.queryParam = {
        pageNum: 1,
        pageSize: 5,
        orderField: undefined,
        orderRule: 'desc',
        collegeName: ''
      }
      this.pageCollegeList()
    },
    /**
     * 学院信息排序
     * @param data 排序信息
     */
    async sortChangeCollegeList(data) {
      const { prop, order } = data
      this.queryParam.orderField = prop
      this.queryParam.orderRule = order === 'ascending' ? 'asc' : 'desc'
      await this.pageCollegeList()
    },
    /**
     * 显示修改学院信息对话框
     * @param row 点击行对应的学院信息
     */
    handleShowUpdateCollegeDialog(row) {
      this.collegeFrom.id = row.id
      this.collegeFrom.collegeName = row.collegeName
      this.$nextTick(() => {
        this.showDialog = true
      })
    },
    /**
     * 修改学院信息
     * @param row 点击行对应的学院信息
     */
    handleUpdateCollege(row) {
      this.$refs.collegeFrom.validate(async valid => {
        if (valid) {
          await updateCollege(this.collegeFrom)
          this.showDialog = false
          this.$message.success('修改学院成功')
          await this.pageCollegeList()
        }
      })
    },
    /**
     * 删除学院信息
     * @param row 点击行对应的学院信息
     */
    handleDeleteCollege(row) {
      this.$confirm('此操作将永久删除学院，是否删除？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteCollege(row.id)
        this.$message.success('删除学院成功')
        await this.pageCollegeList()
      }).catch()
    }
  }
}
</script>
